package org.wbd.pklib.db;

import javax.persistence.*;

import static javax.persistence.GenerationType.IDENTITY;
import java.util.HashSet;
import java.util.Set;

/**
 * Klasa reprezentująca obiekt repozytorium,obiekt tej klasy jest obiektowym odzwierciedleniem
 * rekord w tabeli "repo"
 */
@Entity
@Table(name = "repo")
public class Repo implements java.io.Serializable{
	
	private Integer repoId;
	private String name;
	private Set<Item> items = new HashSet<Item>(0);
	private Set<RepoUser> repoUsers = new HashSet<RepoUser>(0); //repo - user associations
	
	public Repo() {
	}

	public Repo(Integer repoId, String name) {
		this.repoId = repoId;
		this.name = name;
	}

	public Repo(Integer repoId, String name, Set<Item> items, Set<RepoUser> repoUsers) {
		this.repoId = repoId;
		this.name = name;
		this.items = items;
		this.repoUsers = repoUsers;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "repoId", unique = true, nullable = false)
	public Integer getRepoId() {
		return repoId;
	}

	public void setRepoId(Integer repoId) {
		this.repoId = repoId;
	}

	@Column(name = "name", nullable = false, length = 32)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "itemJoin.repo", cascade=CascadeType.ALL)
	public Set<Item> getItems() {
		return items;
	}

	public void setItems(Set<Item> items) {
		this.items = items;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "repoUserJoin.repo", cascade=CascadeType.ALL)
	public Set<RepoUser> getRepoUsers() {
		return repoUsers;
	}

	public void setRepoUsers(Set<RepoUser> repoUsers) {
		this.repoUsers = repoUsers;
	}
	@Override
	public boolean equals(Object o){
		if(!(o instanceof Repo))
				return false;

		Repo repo= (Repo)o;	
		return (this.repoId.equals(repo.getRepoId()));
	}
	
	
	

}
